'''
Created on Mar 22, 2012

@author: pedro
'''

import Preprocessor as pre
import HDRGenerators
import HDRNormalizers
import Pulses
import matplotlib.pyplot as plt
from Network import PILayer,OutputLayer
from Neurons import PyramidalNeuron,Interneuron,OutputNeuron
import random

dt = 0.0002

def hdr_test(generator, normalizer, numhdr, numcols, time, v0, Rm, Cm, tau_ref, Vth, V_spike):
    battery = (pre.generate_hdr_battery(numhdr, numcols, generator, normalizer))
    
    test_hdr = battery[random.randint(0,len(battery)-1)]
    
    pulses = []
    
    for x in range(len(test_hdr)):
        rc_pulse = Pulses.RCFilteredPulse(test_hdr[x]*100.0,500.0,10.0,7.0)
        pulses.append([next(rc_pulse) for t in range(time)])
    
    for x in range(len(pulses)):
        pyr1 = PyramidalNeuron(v0,Rm,Cm,tau_ref,Vth,V_spike)
        Vm = pyr1.fire(pulses[x], dt)
        plt.plot(Vm[:])
    
    plt.show()

if __name__ == '__main__':
    
    numPyrs = 10
    numOuts = numPyrs
    numInterneurons = 4*numPyrs
    time = 1000
    
    gen = HDRGenerators.UniformSigmoidHDRGenerator(0.5,5.0)
    norm = HDRNormalizers.FloorHDRNormalizer()
    battery = (pre.generate_hdr_battery(5, numPyrs, gen, norm))

    hdr = battery[random.randint(0,len(battery)-1)]

    interneurons = []
    pyramidals = []
    outputs = []
    
    for i in range(numOuts):
        o1 = OutputNeuron(-0.07,0.01,1.0,dt*8,1.0,0.5)
        outputs.append(o1)
    
    for i in range(numInterneurons):
        i1 = Interneuron(-0.07,0.01,1.0,1.0)
        interneurons.append(i1)
    
    for i in range(numPyrs):
        pyr1 = PyramidalNeuron(-0.07,0.01,1.0,dt*4,1.0,0.5)
        pyramidals.append(pyr1)

    pilayer = PILayer(pyramidals, interneurons)
    outlayer = OutputLayer(pyramidals, outputs)
    
    for trials in range(20):
        
        pilayer.reset_neurons()
        outlayer.reset_neurons()
        
        pi_layer_outputs = pilayer.fire(hdr, dt, time)
        
        print ""
        for i in pi_layer_outputs:
            print i
        print ""
        
        output_layer_outputs = outlayer.fire(pi_layer_outputs, dt, time)    
    
    #plt.show()
    #pi_layer_outputs = [(x,val) for x,val in enumerate(pi_layer_outputs)]

'''
if __name__ == '__main__':
    gen = HDRGenerators.UniformSigmoidHDRGenerator(0.5,5.0)
    norm = HDRNormalizers.FloorHDRNormalizer()
    battery = (pre.generate_hdr_battery(5, 20, gen, norm))
    
    time = 1000
    
    hdr_test(gen,norm,5,20,time,-0.07,0.01,1.0,dt*4,1.0,0.5)
    
    rcpulse = Pulses.RCFilteredPulse(250.0,500.0,10.0,7.0)
    squarepulse = Pulses.SquarePulse(250.0,500.0)

    rc_pulse = [next(rcpulse) for x in range(time)]
    square_pulse = [next(squarepulse) for x in range(time)]

    input_pulse = rc_pulse

    i1 = neuron.Interneuron(-0.07,0.01,1.0,1.0)
    Vm = i1.fire(input_pulse, dt)
    
    plt.plot([val/1000.0 for val in input_pulse])
    plt.plot(Vm)
    plt.show()
'''